package org.jeecg.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.service.ITigDeviceService;
import org.jeecg.modules.entity.TigDevice;
import org.jeecg.modules.mapper.TigDeviceMapper;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 设备
 * @Author: jeecg-boot
 * @Date:   2022-03-09
 * @Version: V1.0
 */
@Service
public class TigDeviceServiceImpl extends ServiceImpl<TigDeviceMapper, TigDevice> implements ITigDeviceService {
   //重写批量存储，添加不能覆盖同名设备规则，用于设备批量导入识别
    //author：gjs 20230916
    @Override
    public boolean saveBatch(Collection<TigDevice> entityList, int batchSize) {
        //当前存在名字相同的设备则不导入
        LambdaQueryWrapper<TigDevice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TigDevice::getIson, 1);
        wrapper.orderByDesc(TigDevice::getCreateTime);
        List<TigDevice> curTigDevs = this.list(wrapper);
        List<TigDevice> importTigDevs = new ArrayList<>();
        for (TigDevice imTigDev : entityList) {
            Boolean isExistSameDev = false;
            for (TigDevice curTigDevice : curTigDevs){
                if (imTigDev.getName().compareTo(curTigDevice.getName()) == 0 ||
                        (imTigDev.getIpAddr().compareTo(curTigDevice.getIpAddr())==0 &&
                                imTigDev.getPort().compareTo(curTigDevice.getPort())==0)){
                    isExistSameDev = true;
                    break;
                }
            }
            if(!isExistSameDev){
                importTigDevs.add(imTigDev);
            }
        }
        return super.saveBatch(importTigDevs,batchSize);
    }
}
